<?php
class LoginForm extends CFormModel {
	public $username;
	public $password;
	public $rememberMe=FALSE;

	public function rules() {
		return array(
			array('username, password','required'),
			array('rememberMe','boolean'),
			array('password','authenticate'),
		);
	}
	public function attributeLabels() {
		return array(
			'rememberMe' => 'Remember me next time',
		);
	}
	public function authenticate() {
		if(!$this->hasErrors()) {
			$identity = new UserIdentity($this->username,$this->password);
			$identity->authenticate();
			switch($identity->errorCode) {
			case UserIdentity::ERROR_NONE:
				Yii::app()->user->login($identity,$this->rememberMe ? 3600*24*14 : 0);
				return TRUE;
			case UserIdentity::ERROR_USERNAME_INVALID:
				$this->addError('username','Username doesn\'t exist');
				break;
			case UserIdentity::ERROR_PASSWORD_INVALID:
				$this->addError('password','Incorrect password');
				break;
			}
			return FALSE;
		}
	}
}
